package com.mscsoft.ui;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.io.IOException;

import com.mscsoft.App;
import com.mscsoft.tools.ConTools;
import com.mscsoft.tools.Msg;
import com.mscsoft.tools.MyHTTP;
import com.mscsoft.tools.WebTools;

public class Main extends Application {
    Stage homeStage;
    Stage registerStage;
    Stage forgetPwdStage;
    TextField emailText;
    PasswordField pwdText;

    @Override
    public void start(Stage primaryStage) throws IOException {
        homeStage = primaryStage;
        Parent root = new FXMLLoader().load(getClass().getResourceAsStream("/fxml/sample.fxml"));
        Parent register = new FXMLLoader().load(getClass().getResourceAsStream("/fxml/register.fxml"));
        Parent forget = new FXMLLoader().load(getClass().getResourceAsStream("/fxml/forgetPwd.fxml"));
        Label registerLabel = (Label) root.lookup("#registeredLabel");
        Label forgetPwdLabel = (Label) root.lookup("#forgetpwdLabel");
        HBox emailPane = (HBox) root.lookup("#emailPane");
        HBox pwdPane = (HBox) root.lookup("#pwdPane");
        emailText = new TextField((String) App.properties.get("account"));
        emailText.setPrefSize(350, 50);
        emailText.setFont(Font.font("BOLD", 20));
        pwdText = new PasswordField();
        pwdText.setPrefSize(350, 50);
        pwdText.setFont(Font.font("BOLD", 20));
        pwdText.setText((String) App.properties.get("password"));
        emailPane.getChildren().add(emailText);
        pwdPane.getChildren().add(pwdText);
        registerStage = new Stage();
        registerStage.setScene(new Scene(register, 300, 500));
        registerStage.setTitle("RegisterScene");
        forgetPwdStage = new Stage();
        forgetPwdStage.setScene(new Scene(forget, 300, 500));
        forgetPwdStage.setTitle("ForgetScene");
        registerLabel.setOnMouseClicked(this::registerClicked);
        forgetPwdLabel.setOnMouseClicked(this::forgetClicked);
        Button returnToLoginButtonOfReg = (Button) register.lookup("#returnToLoginButton");
        returnToLoginButtonOfReg.setOnAction(e -> returnToLoginButtonOfRegClicked());
        Button returnToLoginButtonOfReS = (Button) forget.lookup("#returnToLoginButton");
        returnToLoginButtonOfReS.setOnAction(e -> returnToLoginButtonOfResClicked());
        Button loginButton = (Button) root.lookup("#loginButton");
        loginButton.setOnAction(e -> loginButtonClicked());
        Button offlineButton = (Button) root.lookup("#offlineButton");
        offlineButton.setOnAction(e -> offlineButtonClicked());
        Button onlineButton = (Button) root.lookup("#onlineButton");
        onlineButton.setOnAction(e -> onlineButtonClicked());
        Button webButton = (Button) root.lookup("#webButton");
        webButton.setOnAction(e -> WebTools.openDownloadWeb());
        primaryStage.setTitle("LoginScene");
        primaryStage.setScene(new Scene(root, App.getResolutionX(), App.getResolutionY()));
        primaryStage.show();
    }

    private void onlineButtonClicked() {
        offline = false;
        App.setOffline(offline);
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Tip");
        alert.setHeaderText("提示");
        alert.setContentText("您已进入在线模式");
        alert.showAndWait();
    }

    private void offlineButtonClicked() {
        offline = true;
        App.setOffline(offline);
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Tip");
        alert.setHeaderText("提示");
        alert.setContentText("您已进入离线模式");
        alert.showAndWait();
    }

    public static void main(String[] args) {
        launch(args);
    }

    boolean offline = App.getOffline();

    public void loginButtonClicked() {
        if (offline) {
            jumpToSelectSongsScene();
        } else {
            String[] args = new String[] { "account=" + emailText.getText(),
                    "password=" + ConTools.sha1(pwdText.getText()) };
            Msg<?> msg = MyHTTP.get("http://" + MyHTTP.serverAddr + "/login", args);
            if (msg != null) {
                if (msg.getCode() == 200) {
                    ConTools.setUid((Integer) (msg.getData()));
                    jumpToSelectSongsScene();
                } else {
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setTitle("警告");
                    alert.setHeaderText("登录失败！");
                    alert.setContentText(msg.getMsg());
                    alert.showAndWait();
                }
            } else {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText("连接服务器失败！");
                alert.setContentText("请稍后重试！");
                alert.showAndWait();
            }
        }
    }

    private void jumpToSelectSongsScene() {
        Platform.runLater(() -> {
            try {
                SelectSongsScene selectSongsScene = new SelectSongsScene();
                selectSongsScene.start(SelectSongsScene.getStage());
                homeStage.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public void registerClicked(MouseEvent e) {
        if (offline)
            offlineWaring();
        else
            registerStage.show();
    }

    public void returnToLoginButtonOfRegClicked() {
        registerStage.close();
    }

    public void forgetClicked(MouseEvent e) {
        if (offline)
            offlineWaring();
        else
            forgetPwdStage.show();
    }

    public void returnToLoginButtonOfResClicked() {
        forgetPwdStage.close();
    }

    private void offlineWaring() {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setHeaderText("离线模式");
        alert.setContentText("您处于离线模式, 请直接点击登录！");
        alert.showAndWait();
    }
}
